package com.mobis.friend.controller;


import com.mobis.friend.FriendServer;
import com.mobis.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import utils.JwtUtils;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtUtils jwtUtils;


    /**
     * 添加好友
     * @param friendid
     * @param type
     * @return
     */
    @RequestMapping(name = "/like/{friendid}/{type}",method = RequestMethod.PUT)
    public Result addFriend(@PathVariable String friendid,@PathVariable String type){

        Claims claims=(Claims) request.getAttribute("user_claims");

        if (null==claims){
         return new Result(false,StatusCode.ERROR,"您无权访问");
        }

        if ("1".equals(type)){
            if (friendService.addFriend(claims.getId(),friendid)==0){
                return new Result(false,StatusCode.ERROR,"您已添加");
            }
        }else {
            friendService.addNoFriend(claims.getId(),friendid);
        }
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     * 删除好友
     * @param friendid
     * @return
     */
    @RequestMapping(value = "/{friendid}",method = RequestMethod.DELETE)
    public Result removeFriend(@PathVariable String friendid){

        Claims claims=(Claims)request.getAttribute("user_claims");
        if (null==claims){
            return new Result(false,StatusCode.ERROR,"您无权删除");
        }

        friendService.deleteFriend(claims.getId(),friendid);
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
